<?php
/**
 * Joomla! 1.5 component sialpacadb
 *
 * @version $Id: controller.php 2010-11-15 11:24:35 svn $
 * @author ErreCi
 * @package Joomla
 * @subpackage sialpacadb
 * @license Copyright (c) 2010 - All Rights Reserved
 *
 * Gestione archivio per allevamenti di Alpaca
 *
 */
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.controller' );
require_once( JPATH_COMPONENT.DS.'helpers'.DS.'helper.php' );

/**
 * sialpacadb Controller
 *
 * @package Joomla
 * @subpackage sialpacadb
 */

class SialpacadbController extends JController {

    /**
     * Constructor
     * @access private
     * @subpackage sialpacadb
     */
    function __construct() {
        //Get View
        if(JRequest::getCmd('view') == '') {
            JRequest::setVar('view', 'addpet');
        }
        $this->item_type = 'Default';
        parent::__construct();
    }
    function display($tmpl = null)
    {
    	$view = JRequest::getVar('view');
    	if( $view == 'editrow' )
    		JRequest::setVar('hidemainmenu', 1);
    	else
    		JRequest::setVar('hidemainmenu', 0);

        parent::display($tmpl);
    }
    function newRow()
    {
    	$data = JRequest::get('post');
 		$link = JRoute::_('index.php?option=com_sialpacadb&view=editrow&tablename='.$data['tablename'].'&pagemode=NEW', false);
	    $this->setRedirect($link);
    }
    function newPet()
    {
    	$data = JRequest::get('post');
 		$link = JRoute::_('index.php?option=com_sialpacadb&view=newpet&tablename='.$data['tablename'].'&pagemode=NEW', false);
	    $this->setRedirect($link);
    }

    function cancelRow()
    {
      	$data = JRequest::get('post');
    	$link = JRoute::_('index.php?option=com_sialpacadb&view=admintab&tablename='.$data['tablename'], false);
	    $this->setRedirect($link);
    }

    function cancelRowPet()
    {
      	$data = JRequest::get('post');
    	$link = JRoute::_('index.php?option=com_sialpacadb&view=addpet&tablename='.$data['tablename'], false);
	    $this->setRedirect($link);
    }

    function saveRow()
    {
        $data = JRequest::get('post');
        $model = $this->getModel('editrow');
        if ($model->store($data)) {
            $message = JText::_('SIA_SAVED_SUCCESSFULLY');
        } else {
            $message = $model->getError();
        }
	    $link = JRoute::_('index.php?option=com_sialpacadb&view=admintab&tablename='.$data['tablename'], false);
	    $this->setRedirect($link, $message);
    }
    function saveRowPet()
    {
        $data = JRequest::get('post');      
        $model = $this->getModel('editrow');
        if ($model->storepet($data)) {
            $message = JText::_('SIA_SAVED_SUCCESSFULLY');
        } else {
            $message = $model->getError();
        }
	    $link = JRoute::_('index.php?option=com_sialpacadb&view=addpet&tablename='.$data['tablename'], false);
	    $this->setRedirect($link, $message);
    }
    function deleteRow()
    {
        $data = JRequest::get('post');
        $model = $this->getModel('editrow');
        if ($model->delete($data)) {
            $message = JText::_('SIA_DELETED_SUCCESFULLY');
        } else {
            $message = $model->getError();
        }
	    $link = JRoute::_('index.php?option=com_sialpacadb&view=admintab&tablename='.$data['tablename'], false);
	    $this->setRedirect($link, $message);
    }
    function saveRowPetax()
    {
        $data = JRequest::get('post');
        $model = $this->getModel('editrow');
        if ($model->storepetax($data)) {
            $message = JText::_('SIA_SAVED_SUCCESSFULLY');
            $session =& JFactory::getSession();
			echo $session->get('insertid');
        } else {
            $message = $model->getError();
            echo false;
        } 
    }
    function getGenAnimal (){
        $data = JRequest::get('get');
        $db 	= &JFactory::getDBO();
        $input = strtolower($data['input'] );
        $tabella = strtolower($data['table'] );
    	$len = strlen($input);
	    $limit = isset($data['limit']) ? (int) $data['limit'] : 0;
        $query = "SELECT nome FROM " .$tabella." WHERE nome LIKE '$input%' LIMIT ". $limit;
        $db->setQuery($query);
        $items = $db->loadAssocList();
        //error_log(print_r($items,true),3,'store.log');
        header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
	    header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
	    header ("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
	    header ("Pragma: no-cache"); // HTTP/1.0
        if (isset($data['json']))
	{
		header("Content-Type: application/json");
	
		echo "{\"results\": [";
		$arr = array();
		for ($i=0;$i<count($items);$i++)
		{
			$arr[] = "{\"id\": \"".($i+1) ."\", \"value\": \"".$items[$i]['nome']."\", \"info\": \"\"}";
		}
		echo implode(", ", $arr);
		echo "]}";
	}
	else
	{
		header("Content-Type: text/xml");

		echo "<?xml version=\"1.0\" encoding=\"utf-8\" ?><results>";
		for ($i=0;$i<count($aResults);$i++)
		{
			echo "<rs id=\"".$items[$i]['id']."\" info=\"".$items[$i]['info']."\">".$items[$i]['value']."</rs>";
		}
		echo "</results>";
	}
	
    }
}

?>

